package com.sskj.xgja.transaction;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.airsaid.pickerviewlibrary.TimePickerView;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.DateUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.MVPActivity;
import com.sskj.xgja.R;
import com.sskj.xgja.transaction.data.OrderHistoryBean;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;

/**
 * @author Hey
 * Create at  2019/03/20
 */
public class HistoryDealActivity extends MVPActivity<HistoryDealPresenter> {
    @BindView(R.id.ll_start_date)
    LinearLayout llStartDate;
    @BindView(R.id.iv_change)
    ImageView ivChange;
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.ll_end_date)
    LinearLayout llEndDate;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.tv_start_date)
    TextView tvStartDate;
    @BindView(R.id.tv_end_date)
    TextView tvEndDate;
    private String startTime;
    private String endTime;

    private BaseAdapter<OrderHistoryBean.OrderHistory> baseAdapter;
    private int page = 1;
    private int pageSize = 10;


    @Override
    public int getLayoutId() {
        return R.layout.activity_history_deal;
    }

    @Override
    public HistoryDealPresenter getPresenter() {
        return new HistoryDealPresenter();
    }

    @Override
    public void initView() {
        initRefreshLayout(smartRefreshLayout);
        toolbar.setLeftButtonOnClickListener(view -> finish());
        toolbar.setTitle("历史成交");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth(20)
                .setDividerColor(Color.parseColor("#00ffffff"))
        );
        baseAdapter = new BaseAdapter<OrderHistoryBean.OrderHistory>(R.layout.item_deal, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, OrderHistoryBean.OrderHistory item) {
                holder.setText(R.id.tv_type, item.getBuyBillType() == 1 ? "做多" : "做空")
                        .setText(R.id.tv_name, item.getStockName())
                        .setText(R.id.tv_buy_price, item.getTradingType() == 0 ? item.getPrice() : item.getBuyPrice())
                        .setText(R.id.tv_sell_price, item.getPrice())
                        .setText(R.id.tv_amount, item.getHandNum() + "")
                        .setText(R.id.tv_profit_loss, item.getWinLoseMoney())
                        .setText(R.id.tv_service_charge, item.getFee())
                        .setText(R.id.tv_promise_money, item.getBuyPrincipal())
                        .setText(R.id.tv_stop_profit, item.getStopProfitPrice())
                        .setText(R.id.tv_stop_loss, item.getStopLossPrice())
                        .setText(R.id.tv_night_price, item.getOverNightFee())
                        .setText(R.id.tv_create_time, item.getCreateTime());

                if (Float.parseFloat(item.getWinLoseMoney()) > 0) {
                    holder.setTextColor(R.id.tv_profit_loss, color(R.color.red));
                } else {
                    holder.setTextColor(R.id.tv_profit_loss, color(R.color.green));
                }

                if (item.getBuyBillType() == 1) {
                    holder.setTextColor(R.id.tv_type, color(R.color.red));
                } else {
                    holder.setTextColor(R.id.tv_type, color(R.color.green));
                }
            }
        };
    }

    @Override
    public void initData() {
        startTime();
        ClickUtil.click(300, llStartDate, view -> startTime(true, tvStartDate));
        ClickUtil.click(300, llEndDate, view -> startTime(false, tvEndDate));
        mPresenter.getHistoryDeal(account, page, pageSize, startTime, endTime);
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getHistoryDeal(account, page, pageSize, startTime, endTime);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getHistoryDeal(account, page, pageSize, startTime, endTime);
            }
        });

    }

    public static void start(Context context) {
        Intent intent = new Intent(context, HistoryDealActivity.class);
        context.startActivity(intent);
    }

    public void startTime(boolean flag, TextView view) {  // true起始时间
        TimePickerView mTimePickerView = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        mTimePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
                String format1 = format.format(date);
                view.setText(format1);
                if (flag) {
                    startTime = format1;
                } else {
                    endTime = format1;
                    if (!DateUtil.isTrueDateTwo(startTime, endTime)) {
                        ToastUtils.show("截止日期小于起始日期，请重新选择");
                        return;
                    }
                    mPresenter.getHistoryDeal(account, page, pageSize, startTime, endTime);
                }
            }
        });
        mTimePickerView.show();
    }

    //默认时间加载
    public void startTime() {
        endTime = DateUtil.getCurrentDate(DateUtil.PATTERN_E);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.WEEK_OF_MONTH, -1);
        startTime = DateUtil.getDateFormat(ca.getTime(), DateUtil.PATTERN_E);
        tvStartDate.setText(startTime);
        tvEndDate.setText(endTime);
    }


    public void setData(OrderHistoryBean data) {
        if (data != null) {
            if (!data.isHasNextPage()) {
                smartRefreshLayout.setNoMoreData(true);
            }
            if (page == 1) {
                baseAdapter.setNewData(data.getList());
            } else {
                baseAdapter.addData(data.getList());
            }
        } else {
            baseAdapter.setNewData(null);
        }
    }

    public void finishRefresh() {
        smartRefreshLayout.finishRefresh();
        smartRefreshLayout.finishLoadMore();
    }

}
